import {post} from "../../request/index.js"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
     id:"",
     ap:false,
     registerMsg:[],
     profession:"",
     messages: [],         // 聊天记录
     msg: '',              // 当前输入
     lastMessageId: '',            // 最后一条消息的ID
     inputContent: '',
      emoji:[
        '😀','😁','😂','😃','😄','😅','😆','😉','😇','😐','😑','😶','😏','😣','😥','😮','😯',
        '😊','😋','😎','😍','😘','😗','😙','😚','😨','😬','😰','😱','😳','😵','😡','😠','😧',
        '😪','😫','😴','😌','😛','😜','😝','😒','😓','😔','😕','😲','😷','😖','😞','😟','😤','😢','😭','😦'],
      sendEmoji:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({id:options.id,profession:options.profession})
      wx.setNavigationBarTitle({
        title: options.profession,
      })
      this.getMsgList();
      this.socketLianjie();
    },

    _emojiItem:function(e){
      let msg= e.currentTarget.dataset.name;
      this.setData({inputContent:msg})
     },

     _helloSend(){
        const uid= wx.getStorageSync('uid');
        if(undefined == uid || null == uid || '' == uid){
          return false;
        }
        const msg = {};
        msg.toUserId = this.data.id
        msg.fromUserId = uid;
        msg.messages = "hello 相遇即是缘，能否认识一下 ！";
        this.sendPostMsg(msg);
     },

  socketLianjie:function(){
    const uid= wx.getStorageSync('uid');
    if(undefined == uid || null == uid || '' == uid){
      return false;
    }
     wx.connectSocket({
       url: 'wss://localhost:9300/websocket/'+uid,
       header:{
        'content-type': 'application/json'
      }
     })
     wx.onSocketMessage((result) => {
      this.getMsgList();
     })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMsgList();
  },

  emojiCheck(){
    if(this.data.sendEmoji){
        this.setData({sendEmoji:false})
    }else{
      this.setData({sendEmoji:true})
    }
  },

  /**
   * 点击「发送」按钮，通过信道推送消息到服务器
   **/
  sendMessage(e) {
    var that = this;
    var inputContent = this.data.inputContent;
    const msg = {};
    msg.toUserId = this.data.id
    const uid= wx.getStorageSync('uid');
    msg.fromUserId = uid;
    if(undefined == uid || null == uid || '' == uid){
      return false;
    }
    msg.type = 0;
    msg.messages = inputContent;
    that.sendPostMsg(msg)
  },

  sendPostMsg(msg){
    post({url:app.globalData.baseUrl+"message/add",data:JSON.stringify(msg)}).then(result=>{
       console.log('result',result.data)
       const res = result.data;
       if(res.code!=0){
         this.setData({registerMsg:res.message})
        }else{
          this.getMsgList();
        }
     })
  },

  getMsgList(){
    const msg = {};
    msg.toUserId = this.data.id
    const uid= wx.getStorageSync('uid');
    msg.fromUserId = uid;
    if(undefined == uid || null == uid || '' == uid){
      return false;
    }

    post({url:app.globalData.baseUrl+"message/list_talk",data:JSON.stringify(msg)}).then(result=>{
       const res = result.data.data;
       if(result.data.code!=0){
         this.setData({registerMsg:res.message})
        }else{
          console.log(res)
          this.setData({messages:res,lastMessageId:'msg_'+(res.length-1),inputContent:""})
        }
     })
  },

  changeInputContent(e) {
    this.setData({
      inputContent: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})